package safeCommitter.core.progress;

import java.lang.reflect.InvocationTargetException;
import java.util.Collections;
import java.util.Set;

import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.swt.widgets.Shell;

import safeCommitter.model.adpters.ISCTest;
import safeCommitter.test.finder.core.ISCTestFinder;
import safeCommitter.utils.SCUtils;

/**
 * @author Luiz Henrique Rolim
 * 
 * Responsible for handling the progress of a test search operation, either in
 * safeCommit or in Run-Related Tests operation
 * 
 */
public class TestSearcherProgressHandler {

	private final ISCTestFinder testFinder;

	public TestSearcherProgressHandler(final ISCTestFinder testFinder) {
		this.testFinder = testFinder;
	}

	public Set<ISCTest> performOperation(final Boolean commitOperation) {
		Shell shell = SCUtils.getShell();
		ProgressMonitorDialog dialog = commitOperation ? new SCProgressMonitorDialog(shell) : new ProgressMonitorDialog(shell);
		try {
			dialog.run(true, true, testFinder);
			dialog.close();
		} catch (InterruptedException e) {
			return getAllTests();
		} catch (InvocationTargetException e) {
			return getAllTests();
		}
		return testFinder.getTestsFound();
	}

	private Set<ISCTest> getAllTests() {
		return Collections.emptySet();
	}

	public Boolean getUserCancelled() {
		return testFinder.getUserCancelled();
	}
}
